package com.ziyun.erp.modules.quoteFactoryMaterial.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ziyun.erp.modules.erpUserLog.entity.ErpUserLogEntity;
import com.ziyun.erp.modules.erpUserLog.service.ErpUserLogService;
import com.ziyun.erp.modules.quoteAttribute.entity.QuoteAttributeOptionEntity;
import com.ziyun.erp.modules.quoteAttribute.service.QuoteAttributeOptionService;
import com.ziyun.erp.modules.quoteFactoryMaterial.dao.QuoteFactoryMaterialDao;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialEntity;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialVO;
import com.ziyun.erp.modules.quoteFactoryMaterial.service.QuoteFactoryMaterialService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("quoteFactoryMaterialService")
public class QuoteFactoryMaterialServiceImpl extends ServiceImpl<QuoteFactoryMaterialDao, QuoteFactoryMaterialEntity> implements QuoteFactoryMaterialService {

    @Autowired
    private QuoteFactoryMaterialDao quoteFactoryMaterialDao;
    @Autowired
    private QuoteAttributeOptionService quoteAttributeOptionService;
    @Autowired
    private ErpUserLogService erpUserLogService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<QuoteFactoryMaterialVO> page = new Page<>();
        page.setCurrent(Integer.valueOf(params.get("page").toString()));
        page.setSize(Integer.valueOf(params.get("limit").toString()));
        List<QuoteFactoryMaterialVO> list = quoteFactoryMaterialDao.queryPage(page, params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public List<QuoteFactoryMaterialEntity> findWeightList(Integer factoryId, String optionName, String materialType) {
        return quoteFactoryMaterialDao.findWeightList(factoryId, optionName, materialType);
    }

    @Override
    @Transactional
    public R insertQuoteFactoryMaterial(List<QuoteFactoryMaterialEntity> quoteFactoryMaterialList,Integer factoryId,String username) {
        Boolean flag=true;
        for (QuoteFactoryMaterialEntity quoteFactoryMaterialEntity : quoteFactoryMaterialList) {
            quoteFactoryMaterialEntity.setMaterialType("shangwuyinshua");
            if (quoteFactoryMaterialDao.getCount(factoryId, quoteFactoryMaterialEntity.getOptionKey(), quoteFactoryMaterialEntity.getMaterialType(),quoteFactoryMaterialEntity.getOptionName()) > 0) {
                flag=false;
                break;
            }
            //查询该材料对应的克重
            List<QuoteFactoryMaterialEntity> list = this.baseMapper.findWeightList(factoryId, quoteFactoryMaterialEntity.getOptionName(), "shangwuyinshua");
            for (QuoteFactoryMaterialEntity temp : list) {
                //查找该克重对应的option_id
                Integer optionId = quoteFactoryMaterialDao.getOptionId(quoteFactoryMaterialEntity.getOptionName(), temp.getOptionName2());
                temp.setOptionId(optionId);
                temp.setFactoryId(factoryId);
                temp.setMaterialType("shangwuyinshua");
                temp.setUpdateDate(new Date());
                if (temp.getFloatingCoefficient() == null) {
                    temp.setFloatingCoefficient(new BigDecimal(1.0));
                }
                temp.setCreateUser(username);
                temp.setUpdateUser(username);
                this.insert(temp);
            }
        }
        if (!flag) {
            return R.error("该材料已添加!");
        } else {
            return R.ok();
        }
    }

    @Override
    public QuoteFactoryMaterialEntity selectOneById(Integer id) {
        QuoteFactoryMaterialEntity quoteFactoryMaterial = this.selectById(id);
        QuoteAttributeOptionEntity quoteAttributeOptionEntity = quoteAttributeOptionService.selectById(quoteFactoryMaterial.getOptionId());
        quoteFactoryMaterial.setOptionName(quoteAttributeOptionEntity.getOptionName());
        quoteFactoryMaterial.setOptionKey(quoteAttributeOptionEntity.getOptionKey());
        return quoteFactoryMaterial;
    }

    @Override
    @Transactional
    public R updateQuoteFactoryMaterialById(List<QuoteFactoryMaterialEntity> quoteFactoryMaterialList,Integer factoryId,String username) {
        for (QuoteFactoryMaterialEntity quoteFactoryMaterialEntity : quoteFactoryMaterialList) {
            if (quoteFactoryMaterialEntity.getId() != null) {
                quoteFactoryMaterialEntity.setUpdateUser(username);
                this.baseMapper.updateById(quoteFactoryMaterialEntity);
            } else {
                //新增
                quoteFactoryMaterialEntity.setFactoryId(factoryId);
                quoteFactoryMaterialEntity.setOptionId(quoteFactoryMaterialEntity.getQaoId());
                quoteFactoryMaterialEntity.setMaterialType("shangwuyinshua");
                quoteFactoryMaterialEntity.setCreateUser(username);
                quoteFactoryMaterialEntity.setUpdateUser(username);
                this.baseMapper.insert(quoteFactoryMaterialEntity);
            }
        }
        return R.ok();
    }

    @Override
    @Transactional
    public Integer deleteByIds(Integer[] ids) {
        return quoteFactoryMaterialDao.deleteByIds(ids);
    }

    @Override
    public int setStart(Integer id, Boolean value) {
        if (value) {
            QuoteFactoryMaterialEntity entity = new QuoteFactoryMaterialEntity();
            entity.setDisplay(false);
            entity.setId(id);
            this.updateById(entity);
        } else {
            QuoteFactoryMaterialEntity entity = new QuoteFactoryMaterialEntity();
            entity.setDisplay(true);
            entity.setId(id);
            this.updateById(entity);
        }
        return 0;
    }

    @Override
    public List<QuoteFactoryMaterialVO> getLeftTableData(Integer factoryId) {
      return quoteFactoryMaterialDao.getLeftTableData(factoryId);
    }

    @Override
    public R delWeight(String optionName, Integer factoryId) {
        //根据工厂id和optionId逻辑删除
        int count = quoteFactoryMaterialDao.delWeight(optionName, factoryId);

        if (count > 0) {
            //监控开始
            ErpUserLogEntity erpUserLogEntity = new ErpUserLogEntity();
            erpUserLogEntity.setUserId(factoryId);
            erpUserLogEntity.setLog("删除,工厂id="+factoryId+",删除"+optionName);
            erpUserLogService.insert(erpUserLogEntity);
            //监控结束
            return R.ok();
        }else {
            return R.error("删除失败");
        }
    }

    @Override
    public void specialPaper(Integer[] ids) {
        this.baseMapper.specialPaper(ids);
    }

    @Override
    public void unSpecialPaper(Integer[] ids) {
        this.baseMapper.unSpecialPaper(ids);
    }


}
